package com.rchat.pocmini.activity.chatmessage;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.airtalkee.sdk.AirtalkeeAccount;
import com.airtalkee.sdk.entity.AirMessage;
import com.bumptech.glide.Glide;
import com.hyphenate.easeui.widget.emojicon.utils.EaseSmileUtils;
import com.rchat.pocmini.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ljd on 2017/9/28.
 *
 *
 */

public class ChatMessageAdapter extends BaseAdapter {

    public interface OnMessageItemClickedListener{
        void onClick(View v);
    }

    private OnMessageItemClickedListener clickedListener;

    private Context mContext;
    private List<AirMessage> messageList = new ArrayList<AirMessage>();

    private int mMinItemWith;// 设置对话框的最大宽度和最小宽度
    private int mMaxItemWith;

    private View viewanim;

    public ChatMessageAdapter(Context context, List<AirMessage> messages,OnMessageItemClickedListener listener) {
        mContext = context;
        messageList = messages;
        clickedListener = listener;

        // 获取系统宽度
        WindowManager wManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wManager.getDefaultDisplay().getMetrics(outMetrics);
        mMaxItemWith = (int) (outMetrics.widthPixels * 0.35f);
        mMinItemWith = (int) (outMetrics.widthPixels * 0.15f);
    }

    @Override
    public int getCount() {
        if (messageList == null) {
            return 0;
        }
        return messageList.size();
    }

    @Override
    public Object getItem(int position) {
        if (messageList == null) {
            return null;
        }
        return messageList.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        AirMessage localAirMessage = (AirMessage)getItem(position);
        ViewHolder localViewHolder;
        String str1 = localAirMessage.getIpocidFrom();
        if (!str1.equals(AirtalkeeAccount.getInstance().getUserId())){
            convertView = buildMessageItemWithOther(position, convertView, localAirMessage);
        }else {
            convertView = buildMessageItemWithMe(position, convertView, localAirMessage);
        }
        localViewHolder = (ViewHolder) convertView.getTag();
        if (needShowDateline(position)) {
            localViewHolder.date.setVisibility(View.VISIBLE);
            localViewHolder.date.setText(localAirMessage.getDate());
        }else {
            localViewHolder.date.setVisibility(View.GONE);
        }
        localViewHolder.time.setText(localAirMessage.getTime());
        return convertView;
    }

    private View buildMessageItemWithOther(int paramInt, View paramView, AirMessage paramAirMessage){
        int msgType = paramAirMessage.getType();
        final ViewHolder2 localViewHolder2;
        if ((paramView == null) || (!(paramView.getTag() instanceof ViewHolder2))){
            paramView = ((Activity) mContext).getLayoutInflater().inflate(R.layout.listitem_conversation_message_other, null);
            localViewHolder2 = new ViewHolder2();
            localViewHolder2.ViewHolderInit(paramView);
            localViewHolder2.download_btn = ((TextView)paramView.findViewById(R.id.downlaod_btn));
            localViewHolder2.unRead = ((ImageView)paramView.findViewById(R.id.un_read));
            localViewHolder2.userName = ((TextView)paramView.findViewById(R.id.user_name));
            paramView.setTag(localViewHolder2);
        }else {
            localViewHolder2 = (ViewHolder2)paramView.getTag();
        }

        localViewHolder2.msg_ptt.setVisibility(View.GONE);
        if (msgType == AirMessage.TYPE_RECORD) {
            localViewHolder2.tvAndIvLayout.setVisibility(View.GONE);
            localViewHolder2.record_time.setText(paramAirMessage.getImageLength() + "\"");
            localViewHolder2.record_time.setVisibility(View.VISIBLE);
            ViewGroup.LayoutParams lParams=localViewHolder2.record_layout.getLayoutParams();
            lParams.width=(int) (mMinItemWith+mMaxItemWith/60f*paramAirMessage.getImageLength());
            localViewHolder2.record_layout.setLayoutParams(lParams);
            localViewHolder2.record_layout.setVisibility(View.VISIBLE);

            if (paramAirMessage.getRecordType() == 1) {
                localViewHolder2.msg_ptt.setVisibility(View.VISIBLE);
            }else {
                localViewHolder2.msg_ptt.setVisibility(View.GONE);
            }

            viewanim = localViewHolder2.record_default;
            if (paramAirMessage.isRecordPlaying()) {
                localViewHolder2.record_default.setBackgroundResource(R.drawable.record_play_other);
                viewanim.setBackgroundResource(R.drawable.record_play_other);
                AnimationDrawable drawable = (AnimationDrawable) viewanim.getBackground();
                drawable.start();
            }else {
                viewanim.setBackgroundResource(R.drawable.record_play_other_3);
                viewanim = null;
                localViewHolder2.record_default.setBackgroundResource(R.drawable.record_play_other_3);
            }

        }else if (msgType == AirMessage.TYPE_TEXT) {
            localViewHolder2.body.setText(EaseSmileUtils.getSmiledText(mContext, paramAirMessage.getBody()));
            localViewHolder2.record_layout.setVisibility(View.GONE);
            localViewHolder2.tvAndIvLayout.setVisibility(View.VISIBLE);
            localViewHolder2.body.setVisibility(View.VISIBLE);
            localViewHolder2.record_time.setVisibility(View.GONE);
            localViewHolder2.pic.setVisibility(View.GONE);
        }else if (msgType == AirMessage.TYPE_PICTURE){
            Glide.with(mContext)
                    .load(paramAirMessage.getImageUri())
                    .error(R.drawable.camera_default)
                    .placeholder(R.drawable.camera_default)
                    .into(localViewHolder2.pic);
            localViewHolder2.pic.setVisibility(View.VISIBLE);
            localViewHolder2.tvAndIvLayout.setVisibility(View.VISIBLE);
            localViewHolder2.body.setVisibility(View.GONE);
            localViewHolder2.record_layout.setVisibility(View.GONE);
            localViewHolder2.record_time.setVisibility(View.GONE);
            if (paramAirMessage.getState() == AirMessage.STATE_DOWNLOADING){
                localViewHolder2.pro.setVisibility(View.VISIBLE);
            }else {
                localViewHolder2.pro.setVisibility(View.GONE);
            }
        }

        localViewHolder2.userName.setText(paramAirMessage.getInameFrom());
        localViewHolder2.body.setText(EaseSmileUtils.getSmiledText(mContext, paramAirMessage.getBody()));
        localViewHolder2.bodyContent.setTag(paramAirMessage);
        localViewHolder2.bodyContent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(clickedListener!=null) {
                    clickedListener.onClick(v);
                }
            }
        });
        return paramView;
    }

    private View buildMessageItemWithMe(int paramInt, View paramView, AirMessage paramAirMessage){
        int msgType = paramAirMessage.getType();
        final ViewHolder1 localViewHolder1;
        if ((paramView == null) || (!(paramView.getTag() instanceof ViewHolder1))){
            paramView = ((Activity) mContext).getLayoutInflater().inflate(R.layout.listitem_conversation_message_me, null);
            localViewHolder1 = new ViewHolder1();
            localViewHolder1.ViewHolderInit(paramView);
            localViewHolder1.report_icon = ((ImageView)paramView.findViewById(R.id.report_icon));
            paramView.setTag(localViewHolder1);
        }else {
            localViewHolder1 = (ViewHolder1)paramView.getTag();
        }

        localViewHolder1.msg_ptt.setVisibility(View.GONE);
        if (msgType == AirMessage.TYPE_RECORD) {
            localViewHolder1.tvAndIvLayout.setVisibility(View.GONE);

            ViewGroup.LayoutParams lParams=localViewHolder1.record_layout.getLayoutParams();
            lParams.width=(int) (mMinItemWith+mMaxItemWith/60f*paramAirMessage.getImageLength());
            localViewHolder1.record_layout.setLayoutParams(lParams);
            localViewHolder1.record_layout.setVisibility(View.VISIBLE);

            if (paramAirMessage.getRecordType() == 1) {
                localViewHolder1.msg_ptt.setVisibility(View.VISIBLE);
            }else {
                localViewHolder1.msg_ptt.setVisibility(View.GONE);
            }

            if (paramAirMessage.getState() == AirMessage.STATE_GENERATING) {
                localViewHolder1.record_time.setVisibility(View.GONE);
                localViewHolder1.record_default.setVisibility(View.GONE);
            }else {
                localViewHolder1.record_time.setText(paramAirMessage.getImageLength() + "\"");
                localViewHolder1.record_time.setVisibility(View.VISIBLE);
                localViewHolder1.record_default.setVisibility(View.VISIBLE);
                viewanim = localViewHolder1.record_default;
                if (paramAirMessage.isRecordPlaying()) {
                    localViewHolder1.record_default.setBackgroundResource(R.drawable.record_play_me);
                    viewanim.setBackgroundResource(R.drawable.record_play_me);
                    AnimationDrawable drawable = (AnimationDrawable) viewanim.getBackground();
                    drawable.start();
                }else {
                    viewanim.setBackgroundResource(R.drawable.record_play_me_3);
                    viewanim = null;
                    localViewHolder1.record_default.setBackgroundResource(R.drawable.record_play_me_3);
                }
            }

        }else if (msgType == AirMessage.TYPE_TEXT) {
            localViewHolder1.body.setText(EaseSmileUtils.getSmiledText(mContext, paramAirMessage.getBody()));
            localViewHolder1.record_layout.setVisibility(View.GONE);
            localViewHolder1.tvAndIvLayout.setVisibility(View.VISIBLE);
            localViewHolder1.body.setVisibility(View.VISIBLE);
            localViewHolder1.record_time.setVisibility(View.GONE);
            localViewHolder1.pic.setVisibility(View.GONE);
        }else if (msgType == AirMessage.TYPE_PICTURE){
            localViewHolder1.pic.setVisibility(View.VISIBLE);
            localViewHolder1.tvAndIvLayout.setVisibility(View.VISIBLE);
            localViewHolder1.body.setVisibility(View.GONE);
            localViewHolder1.record_layout.setVisibility(View.GONE);
            localViewHolder1.record_time.setVisibility(View.GONE);
            if (paramAirMessage.getState() == AirMessage.STATE_SENDING){
                localViewHolder1.pro.setVisibility(View.VISIBLE);
                localViewHolder1.report_icon.setVisibility(View.GONE);
            }else if (paramAirMessage.getState() == AirMessage.STATE_RESULT_FAIL){
                localViewHolder1.report_icon.setVisibility(View.VISIBLE);
                localViewHolder1.pro.setVisibility(View.GONE);
            }else {
                localViewHolder1.report_icon.setVisibility(View.GONE);
                localViewHolder1.pro.setVisibility(View.GONE);
            }
            Glide.with(mContext)
                    .load(paramAirMessage.getImageUri())
                    .error(R.drawable.camera_default)
                    .placeholder(R.drawable.camera_default)
                    .into(localViewHolder1.pic);
        }
        localViewHolder1.bodyContent.setTag(paramAirMessage);
        localViewHolder1.bodyContent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(clickedListener!=null) {
                    clickedListener.onClick(v);
                }
            }
        });
        return paramView;
    }

    private boolean needShowDateline(int paramInt){
        if (paramInt == 0) {
            return true;
        }
        boolean bool = messageList.get(paramInt - 1).getDate().equals(messageList.get(paramInt).getDate());
        return !bool;
    }

    abstract class ViewHolder{
        protected TextView body;
        protected View bodyContent;
        protected View bodyLayout;
        protected TextView date;
        protected ImageView loading;
        protected ImageView msg_ptt;
        protected ImageView pic;
        protected ProgressBar pro;
        protected View record_default;
        protected View record_layout;
        protected TextView record_time;
        protected TextView time;
        protected View tvAndIvLayout;
        protected TextView tvSystem;
        protected ImageView userHead;

        ViewHolder()
        {
        }

        protected void ViewHolderInit(View paramView){
            this.time = ((TextView)paramView.findViewById(R.id.time));
            this.body = ((TextView)paramView.findViewById(R.id.body));
            this.date = ((TextView)paramView.findViewById(R.id.sessionDate));
            this.pic = ((ImageView)paramView.findViewById(R.id.pic));
            this.userHead = ((ImageView)paramView.findViewById(R.id.user_head));
            this.loading = ((ImageView)paramView.findViewById(R.id.loading));
            this.pro = ((ProgressBar)paramView.findViewById(R.id.pro_load));
            this.record_time = ((TextView)paramView.findViewById(R.id.record_time));
            this.record_default = (paramView.findViewById(R.id.record_pic));
            this.record_layout = paramView.findViewById(R.id.record_layout);
            this.tvAndIvLayout = paramView.findViewById(R.id.text_and_picture_layout);
            this.bodyLayout = paramView.findViewById(R.id.body_layout);
            this.bodyContent = paramView.findViewById(R.id.body_content);
            this.tvSystem = ((TextView)paramView.findViewById(R.id.tv_system));
            this.msg_ptt = ((ImageView)paramView.findViewById(R.id.msg_ptt));
        }
    }

    protected class ViewHolder1 extends ViewHolder{
        ImageView report_icon;

        protected ViewHolder1(){
            super();
        }
    }

    class ViewHolder2 extends ViewHolder{
        TextView download_btn;
        ImageView unRead;
        TextView userName;

        ViewHolder2() {
            super();
        }
    }
}

